﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;

namespace MusicCatalogue
{
    class MusicCatalogueUtils
    {
        private string inputFile;

        public MusicCatalogueUtils(string inputFile)
        {
            this.inputFile = inputFile;
        }

        public string InputFile
        {
            get
            {
                return inputFile;
            }
            private set
            {
                inputFile = value;
            }
        }

        public List<string> GetSongTitles() 
        {
            string tagNameForSongs = "title";
            List<string> songTitles = new List<string>();
            using (XmlReader reader = XmlReader.Create(this.InputFile)) 
            {
                while (reader.Read()) 
                {
                    if (reader.Name == tagNameForSongs && reader.NodeType == XmlNodeType.Element) 
                    {
                        while (!reader.HasValue)
                        {
                            reader.Read();
                        }
                        songTitles.Add(reader.Value.Trim());
                    }
                }
            }
            return songTitles;
        }

        public List<string> GetSongTitlesWithLinq() 
        {
            List<string> songTitles = new List<string>();

            //XDocument catalogue = XDocument.Load("catalogue.xml");
            //var songNames =
            //    from song in catalogue.Descendants("song")
            //    select song.Element("title").Value;
            //foreach (var song in songNames)
            //{
            //    Console.WriteLine(song.Trim());
            //}

            return songTitles;
        }

        public void DeleteCheapAlbums() 
        {
            XmlDocument catalogue = new XmlDocument();
            catalogue.Load(this.InputFile);

            XmlNode rootNode = catalogue.DocumentElement;
            string XPathQuerry = "/catalogue/*";
            XmlNodeList albumList = catalogue.SelectNodes(XPathQuerry);

            foreach (XmlNode node in albumList) 
            {
                if (decimal.Parse(node["price"].InnerText)>20) 
                {
                    rootNode.RemoveChild(node);
                }
            }
            catalogue.Save("NewCatalogue.xml");
        }

        public void ExtractArtistsAndNumberOfSongs() 
        {
            XmlDocument catalogue = new XmlDocument();
            catalogue.Load(this.InputFile);
            Dictionary<string, int> numberOfSongByArtist = new Dictionary<string, int>();

            string XPathQuerry = "/catalogue/*";

            XmlNodeList albumList = catalogue.SelectNodes(XPathQuerry);

            foreach (XmlNode album in albumList)
            {
                string artist = album["artist"].InnerText.Trim();
                ProcessAlbum(artist, numberOfSongByArtist, album);
            }

            PrintResults(numberOfSongByArtist);
        }
  
        private void PrintResults(Dictionary<string, int> numberOfSongByArtist)
        {
            foreach (var entry in numberOfSongByArtist) 
            {
                Console.WriteLine("artist: {0}\n number of songs: {1}", entry.Key, entry.Value);
            }
        }
  
        private void ProcessAlbum(string artist, Dictionary<string, int> numberOfSongByArtist, XmlNode album)
        {
            if (numberOfSongByArtist.ContainsKey(artist))
            {
                AddNewSongs(album, numberOfSongByArtist, artist);
            }
            else 
            {
                numberOfSongByArtist.Add(artist, 0);
                AddNewSongs(album, numberOfSongByArtist, artist);
            }
        }
  
        private void AddNewSongs(XmlNode album, Dictionary<string, int> numberOfSongByArtist, string artist)
        {
            numberOfSongByArtist[artist] += album["songs"].ChildNodes.Count;
        }
    }
}
